package com.hongxun.design.utils.shareutil;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;

import com.hongxun.design.R;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.mytools.ToastShow;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

/**
 * Created by zerdoor_pc on 2016/8/3.
 */
public class ShareUtil {

    static ShareUtil instence;

    public static ShareUtil getInstence() {
        if (instence == null) {
            instence = new ShareUtil();
        }
        return instence;
    }

    /**
     * 初始化配置*
     */
    public void InIt() {
        PlatformConfig.setWeixin(Constant.WXAPPID,Constant.WXAPPSERC);
        //微信 appid appsecret
        PlatformConfig.setQQZone(Constant.QQAPPID, Constant.QQAPPSERC);
        //QQ和Qzone appid appkey
//        PlatformConfig.setSinaWeibo("3921700954","04b48b094faeb16683c32669824ebdad");
//        //新浪微博 appkey appsecret
//        PlatformConfig.setAlipay("2015111700822536");
//        //支付宝 appid
//        PlatformConfig.setYixin("yxc0614e80c9304c11b0391514d09f13bf");
//        //易信 appkey
//        PlatformConfig.setTwitter("3aIN7fuF685MuZ7jtXkQxalyi", "MK6FEYG63eWcpDFgRYw4w9puJhzDl0tyuqWjZ3M7XJuuG7mMbO");
//        //Twitter appid appkey
//        PlatformConfig.setPinterest("1439206");
//        //Pinterest appid
//        PlatformConfig.setLaiwang("laiwangd497e70d4", "d497e70d4c3e4efeab1381476bac4c5e");
//        //来往 appid appkey
    }


    /**
     * 默认分享*
     */
    public void shareboard(Activity activity, String name, String url, String text, String image) {
//        title参数对新浪、人人、豆瓣不生效
        final SHARE_MEDIA[] displaylist = new SHARE_MEDIA[]
                {
                        SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE,SHARE_MEDIA.QQ
                };
        new ShareAction(activity).setDisplayList(displaylist)
                .withText(text)
                .withTitle(name)
                .withTargetUrl(url)
                .withMedia(new UMImage(activity, image))
                .setListenerList(new UmShareReback(activity))
                .open();
    }

    /**
     * 自定义分享*
     */
    public void shareCustom(Activity activity, SHARE_MEDIA shareType, String name, String url, String text, String path) {
        MyLog.L(MString.SUCCESSED + " shareType=" + shareType + "\nname=" + name + "\nurl=" + url + "\ntext=" + text + "\nimage=" + path);
        try {
            if (path==null||name==null)return;
            UMImage  image;
            if (path.length()==0){
                image = new UMImage(activity, BitmapFactory.decodeResource(activity.getResources(), R.mipmap.icon));
            }else{
                image = new UMImage(activity,path);
            }


            new ShareAction(activity)
                    .setPlatform(shareType)
                    .setCallback(new UmShareReback(activity))
                    .withTitle(name)
                    .withText(text)
                    .withTargetUrl(url)
                    .withMedia(image)
                    .share();
        }catch (Exception e){
            MyLog.L(MString.SUCCESSED +e.toString());
        }

    }

    private   final int Resourse = 0;
    private   final int SD = 1;
    private   final int URL = 2;

    public   UMImage getUri(Activity activity, String path, int type) {
        UMImage image=null;
        switch (type) {
            case Resourse:
                image = new UMImage(activity, BitmapFactory.decodeResource(activity.getResources(),Integer.parseInt(path)));
                break;
            case SD:
                image = new UMImage(activity,BitmapFactory.decodeFile(path));
                break;
            case URL:
                image = new UMImage(activity, path);
                break;

        }
        return image;

    }

    class UmShareReback implements UMShareListener {
        Context context;

        UmShareReback(Context context) {
            this.context = context;
        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            ToastShow.showShort(context, platform + " 分享成功啦");
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            ToastShow.showShort(context, platform + " 分享失败啦");
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            ToastShow.showShort(context, platform + " 分享取消了");
        }
    }


}
